#include "GBCSolution.h"

GBCSolution::GBCSolution(void){
    sln=NULL;
    memset(&param,sizeof(param),0);
}

GBCSolution::GBCSolution(double* isolution, const char* iname, double ibc, double isightheight, int iweight, int imv, int iangle, int izerorange, int iwindspeed, int iwindangle, int itemp, int ihumidity, double ipressure, int ialtitude, int entries, int useweather, int idf){

	sln=isolution;
	strncpy(param.name,iname,sizeof(param.name));
	param.bc=ibc;
	param.sightheight=isightheight;
	param.weight=iweight;
	param.mv=imv;
	param.angle=iangle;
	param.zerorange=izerorange;
	param.windspeed=iwindspeed;
	param.windangle=iwindangle;
	param.temp=itemp;
	param.humidity=ihumidity;
	param.pressure=ipressure;
	param.altitude=ialtitude;
	param.rows=entries;
	param.ckweather=useweather;
	param.df = idf;
}



GBCSolution::~GBCSolution(){

	if (this->sln != NULL) {
		free(this->sln);
		this->sln=NULL;
	}	

}
		

#define FILEVERSION 0

int GBCSolution::Save(FILE* f) {
  int wrote;
  param.reserved1=0;
  param.reserved2=FILEVERSION;
  wrote=fwrite(&param, 1, sizeof(param), f);
  if (wrote == sizeof(param)) return 1;
  return 0;
}

int GBCSolution::Load(FILE* f) {
  int read;
  
  Clear();
  read=fread(&param, 1, sizeof(param), f);
  
  // file saved by current version?
  if ((read == sizeof(param)) && (param.reserved1==0) && (param.reserved2==FILEVERSION))
     return 1;

  if ((param.reserved1 == 0) && (param.reserved2 < 256)) {
     // not the original format with pointers in it... 
     if (param.reserved2 < FILEVERSION) {
        // default any missing fields based on version number in the file
     }
     return 1;
  }
  else if (read==336) {
    // the original format... the first part of the data is valid, default any new fields

    // no new fields yet :-)
    return 1;
  }
  return 0;
}


bool GBCSolution::Solved()
{
  return sln!=NULL;
}

void GBCSolution::Clear()
{
  if (sln!=NULL) {
      free(sln);
      sln=NULL;
  }
}


const char* GBCSolution::Name(void){
	return param.name;
}
	
double GBCSolution::BC(void){
	return param.bc;
}

double GBCSolution::SightHeight(void){
	return param.sightheight;
}

int GBCSolution::DragFunction(void )
{
	return param.df;
}

int GBCSolution::MuzzleVelocity(void){
	return param.mv;
}

int GBCSolution::ShootingAngle(void){
	return param.angle;
}
int GBCSolution::ZeroRange(void){
	return param.zerorange;
}

int GBCSolution::WindSpeed(void){
	return param.windspeed;
}

int GBCSolution::WindAngle(void){
	return param.windangle;
}

int GBCSolution::Temp(void){
	return param.temp;
}

double GBCSolution::Pressure(void){
	return param.pressure;
}

int GBCSolution::Humidity(void){
	return param.humidity;
}

int GBCSolution::Altitude(void){
	return param.altitude;
}



double GBCSolution::GetRange(int yardage){
	double size=sln[__BCOMP_MAXRANGE__*10+1];
	if (yardage<size){
		return sln[10*yardage];
	}
	else return 0;
}

double GBCSolution::GetPath(int yardage){
	double size=sln[__BCOMP_MAXRANGE__*10+1];
	if (yardage<size){
		return sln[10*yardage+1];
	}
	else return 0;
}

double GBCSolution::GetMOA(int yardage){
	double size=sln[__BCOMP_MAXRANGE__*10+1];
	if (yardage<size){
		return sln[10*yardage+2];
	}
	else return 0;
}


double GBCSolution::GetTime(int yardage){
	double size=sln[__BCOMP_MAXRANGE__*10+1];
	if (yardage<size){
		return sln[10*yardage+3];
	}
	else return 0;
}

double GBCSolution::GetWindage(int yardage){
	double size=sln[__BCOMP_MAXRANGE__*10+1];
	if (yardage<size){
		return sln[10*yardage+4];
	}
	else return 0;
}

double GBCSolution::GetWindageMOA(int yardage){
	double size=sln[__BCOMP_MAXRANGE__*10+1];
	if (yardage<size){
		return sln[10*yardage+5];
	}
	else return 0;
}

double GBCSolution::GetVelocity(int yardage){
	double size=sln[__BCOMP_MAXRANGE__*10+1];
	if (yardage<size){
		return sln[10*yardage+6];
	}
	else return 0;
}

double GBCSolution::GetVx(int yardage){
	double size=sln[__BCOMP_MAXRANGE__*10+1];
	if (yardage<size){
		return sln[10*yardage+7];
	}
	else return 0;
}

double GBCSolution::GetVy(int yardage){
	double size=sln[__BCOMP_MAXRANGE__*10+1];
	if (yardage<size){
		return sln[10*yardage+8];
	}
	else return 0;
}

double GBCSolution::GetDrop(int yardage){
	double size=sln[__BCOMP_MAXRANGE__*10+1];
	if (yardage<size){
		return sln[10*yardage+9];
	}
	else return 0;
}

double GBCSolution::GetEnergy(int k){
	return (double)param.weight*GetVelocity(k)*GetVelocity(k)/450436;
}


int GBCSolution::MaxRows(void){
	return param.rows;
}

int GBCSolution::MaxRows(int mr){
	param.rows=mr;
	return param.rows;
}

int GBCSolution::Weight(void){
	return param.weight;
}

int GBCSolution::UseWeather(void){
	return param.ckweather;
}


